// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) braces deadcode fieldsfirst 

package net.minecraft.util;

import java.io.File;
import java.util.ArrayList;
import javax.swing.filechooser.FileFilter;
import net.minecraft.MinecraftUtil;

// Referenced classes of package anjocaido.minecraftmanager:
//            Zipper

public class BackupUtil
{
    public static class GameFileFilter extends FileFilter
    {

        public boolean accept(File file)
        {
            if(file == null)
            {
                return false;
            }
            if(file.isDirectory())
            {
                return true;
            }
            String s = BackupUtil.getExtension(file);
            return s != null && s.equalsIgnoreCase("mcgame");
        }

        public String getDescription()
        {
            return "Minecraft Game files";
        }

        public GameFileFilter()
        {
        }
    }

    public static class WorldFileFilter extends FileFilter
    {

        public boolean accept(File file)
        {
            if(file == null)
            {
                return false;
            }
            if(file.isDirectory())
            {
                return true;
            }
            String s = BackupUtil.getExtension(file);
            return s != null && s.equalsIgnoreCase("mcworld");
        }

        public String getDescription()
        {
            return "Minecraft World files";
        }

        public WorldFileFilter()
        {
        }
    }


    public static final String WORLD_BACKUP_EXTENSION = "mcworld";
    public static final String WORLD_BACKUP_GEN_NAME = "world_backup";
    public static final String GAME_BACKUP_EXTENSION = "mcgame";
    public static final String GAME_BACKUP_GEN_NAME = "minecraft_backup";
    public static final String DATE_TIME_FORMAT = "%1$tY-%1$tm-%1$td_%1$tH-%1$tM-%1$tS";

    public BackupUtil()
    {
    }

    public static void uninstallGame(boolean flag)
    {
        deleteFileDir(MinecraftUtil.getBinFolder());
        deleteFileDir(MinecraftUtil.getLoginFile());
        deleteFileDir(MinecraftUtil.getResourcesFolder());
        deleteFileDir(MinecraftUtil.getOptionsFile());
        if(flag)
        {
            deleteFileDir(MinecraftUtil.getSavesFolder());
        }
    }

    public static void backupGame(File file, boolean flag)
    {
        File afile[];
        if(!flag)
        {
            ArrayList<File> arraylist = new ArrayList<File>();
            File file1 = MinecraftUtil.getBinFolder();
            if(file1.exists())
            {
                arraylist.add(file1);
            }
            file1 = MinecraftUtil.getResourcesFolder();
            if(file1.exists())
            {
                arraylist.add(file1);
            }
            file1 = MinecraftUtil.getLoginFile();
            if(file1.exists())
            {
                arraylist.add(file1);
            }
            file1 = MinecraftUtil.getOptionsFile();
            if(file1.exists())
            {
                arraylist.add(file1);
            }
            afile = (File[])arraylist.toArray(new File[arraylist.size()]);
        } else
        {
            afile = MinecraftUtil.getWorkingDirectory().listFiles();
        }
        backupContents(afile, file, "minecraft_backup", "mcgame");
    }

    public static void restoreGame(File file)
    {
        File file1 = MinecraftUtil.getWorkingDirectory();
        restoreContents(file, file1, "minecraft_backup");
    }

    public static File getWorldNFolder(int i)
    {
        File file = new File(MinecraftUtil.getSavesFolder(), (new StringBuilder()).append("World").append(i).toString());
        return file;
    }

    public static void backupWorld(int i, File file)
    {
        File file1 = getWorldNFolder(i);
        backupFile(file1, file, "world_backup", "mcworld");
    }

    public static void restoreWorld(int i, File file)
    {
        File file1 = getWorldNFolder(i);
        restoreFile(file, file1, "world_backup");
    }

    public static void backupFile(File file, File file1, String s, String s1)
    {
        File file2 = new File(MinecraftUtil.getTempFolder(), s);
        if(!file.exists())
        {
            throw new IllegalArgumentException((new StringBuilder()).append("Source file does not exist: ").append(file.getName()).toString());
        }
        if(!file1.getName().endsWith((new StringBuilder()).append(".").append(s1).toString()))
        {
            file1 = new File((new StringBuilder()).append(file1.getPath()).append(".").append(s1).toString());
        }
        if(file2.exists())
        {
            deleteFileDir(file2);
        }
        file.renameTo(file2);
        Zipper.zipFolder(file2, file1);
        file2.renameTo(file);
    }

    public static String getExtension(File file)
    {
        String s = null;
        String s1 = file.getName();
        int i = s1.lastIndexOf('.');
        if(i > 0 && i < s1.length() - 1)
        {
            s = s1.substring(i + 1).toLowerCase();
        }
        return s;
    }

    public static boolean deleteFileDir(File file)
    {
        if(!file.exists())
        {
            return false;
        }
        if(file.isFile())
        {
            return file.delete();
        }
        if(file.isDirectory())
        {
            File afile[] = file.listFiles();
            int i = afile.length;
            for(int j = 0; j < i; j++)
            {
                File file1 = afile[j];
                deleteFileDir(file1);
            }

            return file.delete();
        } else
        {
            return file.delete();
        }
    }

    public static void restoreFile(File file, File file1, String s)
    {
        File file2 = new File(MinecraftUtil.getTempFolder(), s);
        if(file2.exists())
        {
            deleteFileDir(file2);
        }
        Zipper.unzipFolder(file, MinecraftUtil.getTempFolder());
        if(!file2.exists())
        {
            throw new IllegalStateException((new StringBuilder()).append("Wrong content in zip file -> not found: ").append(file2.getName()).toString());
        }
        if(file1.exists())
        {
            deleteFileDir(file1);
        }
        if(file1.getParentFile() != null && !file1.getParentFile().exists())
        {
            file1.getParentFile().mkdirs();
        }
        file2.renameTo(file1);
    }

    public static void backupContents(File afile[], File file, String s, String s1)
    {
        File afile1[] = afile;
        int i = afile1.length;
        for(int j = 0; j < i; j++)
        {
            File file1 = afile1[j];
            if(!file1.exists())
            {
                throw new IllegalArgumentException((new StringBuilder()).append("You sent me a folder content that doesnt exist : ").append(file1.getName()).toString());
            }
        }

        if(!file.getName().endsWith((new StringBuilder()).append(".").append(s1).toString()))
        {
            file = new File((new StringBuilder()).append(file.getPath()).append(".").append(s1).toString());
        }
        Zipper.zipFolders(afile, file, s);
    }

    public static void restoreContents(File file, File file1, String s)
    {
        File file2 = new File(MinecraftUtil.getTempFolder(), s);
        if(file2.exists())
        {
            deleteFileDir(file2);
        }
        if(!file1.exists())
        {
            file1.mkdirs();
        }
        if(!file1.isDirectory())
        {
            throw new IllegalArgumentException("The destiny folder must be a directory!");
        }
        Zipper.unzipFolder(file, MinecraftUtil.getTempFolder());
        if(!file2.exists())
        {
            throw new IllegalStateException((new StringBuilder()).append("Wrong content in zip file -> not found: ").append(file2.getName()).toString());
        }
        File afile[] = file2.listFiles();
        File afile1[] = afile;
        int i = afile1.length;
        for(int j = 0; j < i; j++)
        {
            File file3 = afile1[j];
            File file4 = new File(file1, file3.getName());
            if(file4.exists())
            {
                deleteFileDir(file4);
            }
            file3.renameTo(file4);
        }

    }
}
